<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('store_id')->unsigned()->default(0)->comment('店铺id');
            $table->integer('pid')->unsigned()->default(0)->comment('推荐人id');
            $table->integer('left')->default(0)->comment('关系树左值');
            $table->integer('right')->default(0)->comment('关系树右值');
            $table->integer('level')->unsigned()->default(0)->comment('第0代为公司账号,后续代数递增');
            $table->string('true_name')->default('')->comment('真实姓名');
            $table->string('phone')->default('')->comment('手机号');
            $table->string('password')->default('')->comment('登录密码');
            $table->string('password_pay')->default('')->comment('支付密码');
            $table->string('weixin')->default('')->comment('微信号');
            $table->tinyInteger('grade')->default(0)->comment('会员等级，从0开始');
            $table->string('nickname')->default('')->comment('微信昵称');
            $table->string('avatar')->default('')->comment('微信头像');
            $table->string('open_id')->default('')->comment('微信open_id');
            $table->string('union_id')->default('')->comment('微信union_id');
            $table->integer('gold')->unsigned()->default(0)->comment('余额，单位分');
            $table->integer('jf')->unsigned()->default(0)->comment('积分，单位分');
            $table->integer('jgz')->unsigned()->default(0)->comment('军功章，单位分');
            $table->tinyInteger('is_subscribe')->default(0)->comment('是否关注，0未关注，1已关注');
            $table->datetime('subscribe_at')->nullable()->comment('关注时间');
            $table->tinyInteger('is_regist')->default(0)->comment('是否同意会员协议，同意为注册用户，0未注册，1注册');
            $table->datetime('regist_at')->nullable()->comment('注册时间');
            $table->integer('address_id')->unsigned()->default(0)->comment('默认地址id');
            $table->integer('bank_id')->unsigned()->default(0)->comment('默认银行id');
            $table->tinyInteger('is_visitor')->unsigned()->default(1)->comment('是否是游客，1是，0不是');
            $table->timestamps();
            $table->unique('left');
            $table->unique('right');
            $table->unique(['store_id', 'open_id']);
        });

        DB::statement("ALTER TABLE `users` comment '前台用户表'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
